//
//  ReputationInfoVC.m
//  HxgProject
//
//  Created by hxg on 2018/3/7.
//  Copyright © 2018年 XMYY-19. All rights reserved.
//

#import "ReputationInfoVC.h"
#import "ReputationView.h"

#import "RDVTabBarController.h"
#import "AuthenService.h"
#import "ReputationDetail.h"

#import "Masonry.h"
#import "UIColor+HEX.h"
#import "UIButton+Extention.h"
#import "ReputationDetail.h"
#import <IQKeyboardReturnKeyHandler.h>

@interface ReputationInfoVC ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
{
    IQKeyboardReturnKeyHandler *returnKeyHandler;
    
}
// 信誉认证的表格
@property (nonatomic, strong) UITableView *reputationTView;

// 认证的提示信息
@property (nonatomic, strong) NSArray *desStrArr;


//
@property (nonatomic, strong) NSArray *placeholderArr;

@end

@implementation ReputationInfoVC

- (UITableView *)reputationTView
{
    if (!_reputationTView)
    {
        _reputationTView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _reputationTView.delegate = self;
        _reputationTView.dataSource =  self;
        _reputationTView.tableFooterView = [[UIView alloc] init];
        _reputationTView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    
    return _reputationTView;
}

- (NSArray *)desStrArr{
    if (!_desStrArr)
    {
        _desStrArr = [NSArray arrayWithObjects:@"支付宝账号",@"支付宝密码",@"允许授权芝麻信用",@"京东账号",@"京东密码",@"允许京东授权",@"提交认证", nil];
    }
    return _desStrArr;
}

- (NSArray *)placeholderArr
{
    if (!_placeholderArr)
    {
        _placeholderArr = [NSArray arrayWithObjects:@"请输入支付宝账号",@"请输入支付宝密码",@"允许授权芝麻信用",@"请输入京东账号",@"请输入京东密码",@"允许京东授权", nil];
    }
    return _placeholderArr;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"信誉认证";
    [self.view addSubview:self.reputationTView];
    [self.reputationTView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(64.0f);
        make.left.equalTo(self.view.mas_left);
        make.size.mas_equalTo(CGSizeMake(self.view.frame.size.width, self.view.frame.size.height));
    }];
    
    returnKeyHandler = [[IQKeyboardReturnKeyHandler alloc] initWithViewController:self];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
    
    [super viewWillDisappear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDataSource
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        if (0 == indexPath.row
            || 1 == indexPath.row
            || 3 == indexPath.row
            || 4 == indexPath.row )
        {
            UILabel *titleLbl = [[UILabel alloc] init];
            titleLbl.text = self.desStrArr[indexPath.row];
            [cell.contentView addSubview:titleLbl];
            [titleLbl makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(cell.contentView.mas_left).offset(15.0f);
                make.centerY.equalTo(cell.contentView.mas_centerY);
            }];
            
            UITextField *textField = [[UITextField alloc] init];
            textField.tag = indexPath.row;
            textField.placeholder = self.placeholderArr[indexPath.row];
            
            if (0 == indexPath.row) {
                textField.text = self.showReputationInfo.alipayAccount;
            }else if (1 == indexPath.row) {
                [textField setSecureTextEntry:YES];
                if (nil == self.showReputationInfo.alipayAccount
                    || 0 == self.showReputationInfo.alipayAccount.length)
                {
                    textField.text = @"";
                }
                else
                {
                    textField.text = @"1111";
                }
            }
            else if (3 == indexPath.row)
            {
                textField.text = self.showReputationInfo.alipayAccount;
            }
            else if (4 == indexPath.row)
            {
                [textField setSecureTextEntry:YES];
                if (nil == self.showReputationInfo.alipayAccount
                    || 0 == self.showReputationInfo.alipayAccount.length)
                {
                    textField.text = @"";
                }
                else
                {
                    textField.text = @"1111";
                }
            }
            
            textField.delegate = self;
            [textField addTarget:self
                          action:@selector(inputTextField:)
                forControlEvents:UIControlEventEditingChanged];
            
            [cell.contentView addSubview:textField];
            [textField makeConstraints:^(MASConstraintMaker *make) {
                
                make.right.equalTo(cell.contentView.mas_right);
                make.centerY.equalTo(cell.contentView.mas_centerY);
                make.size.mas_equalTo(CGSizeMake(cell.contentView.frame.size.width / 2, 55.0f));
            }];
            
            UIView *lineView = [[UIView alloc] init];
            lineView.backgroundColor = [UIColor colorWithHexString:@"#d9d9d9"];
            [cell.contentView addSubview:lineView];
            [lineView makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(titleLbl.mas_left);
                make.bottom.equalTo(cell.contentView.mas_bottom);
                make.size.mas_equalTo(CGSizeMake(cell.contentView.frame.size.width, 1.0f));
            }];
        }
        
        if (2 == indexPath.row || 5 == indexPath.row || 6 == indexPath.row)
        {
            UIButton * submitBtn  = [[UIButton alloc] init];
            [submitBtn setTitle:self.desStrArr[indexPath.row]
                             color:mainColor
                              font:mainFont];
            [cell.contentView addSubview:submitBtn];
            
            
            if (6 == indexPath.row)
            {
                if ([[NSUserDefaults standardUserDefaults] boolForKey:kXinYuAuthenRes])
                {
                    submitBtn.userInteractionEnabled = NO;//交互关闭
                    submitBtn.alpha = 0.4;//透明度
                }
                else
                {
                    submitBtn.userInteractionEnabled = YES;//交互打开
                    submitBtn.alpha = 1;//透明度
                }
                
                
                [submitBtn addTarget:self
                              action:@selector(submitALlAction:)
                    forControlEvents:UIControlEventTouchUpInside];
            }
            else
            {
                if (2 == indexPath.row) {
                    
                    if ([[NSUserDefaults standardUserDefaults] boolForKey:kXinYuAuthenRes])
                    {
                        submitBtn.userInteractionEnabled = NO;//交互关闭
                        submitBtn.alpha = 0.4;//透明度
                    }
                    else
                    {
                        submitBtn.userInteractionEnabled = YES;//交互打开
                        submitBtn.alpha = 1;//透明度
                    }
                    
                    submitBtn.tag = 2;
                } else if (5 == indexPath.row) {
                    
                    if ([[NSUserDefaults standardUserDefaults] boolForKey:kXinYuAuthenRes])
                    {
                        submitBtn.userInteractionEnabled = NO;//交互关闭
                        submitBtn.alpha = 0.4;//透明度
                    }
                    else
                    {
                        submitBtn.userInteractionEnabled = YES;//交互打开
                        submitBtn.alpha = 1;//透明度
                    }
                    
                    submitBtn.tag = 5;
                }
                
                [submitBtn addTarget:self
                              action:@selector(submitAction:)
                    forControlEvents:UIControlEventTouchUpInside];
            }
            
            [submitBtn makeConstraints:^(MASConstraintMaker *make) {
                
                make.centerX.equalTo(cell.contentView.mas_centerX);
                make.bottom.equalTo(cell.contentView.mas_bottom);
                make.size.mas_equalTo(CGSizeMake(cell.contentView.frame.size.width * 3 / 4,55.0f));
            }];
        }
    }
    
    
    return cell;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.desStrArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (2 == indexPath.row
        || 5 == indexPath.row
        ) {
        return 70;
    }
    
    if (6 == indexPath.row)
    {
        return 100;
    }
    
   return 60.0f;
}

#pragma mark - actions

- (void)inputTextField:(UITextField *)textField
{
    switch (textField.tag) {
        case 0:
            self.showReputationInfo.alipayAccount = textField.text;
            break;
        case 1:
            self.showReputationInfo.alipayPwd = textField.text;
            break;
        case 3:
            self.showReputationInfo.jdAccount = textField.text;
            break;
        case 4:
            self.showReputationInfo.jdPwd = textField.text;
            break;
            
        default:
            break;
    }
    
}

- (void)submitAction:(UIButton *)btn
{
    if (2 == btn.tag)
    {
        if (isEmptyString(self.showReputationInfo.alipayAccount)
            )
        {
            [SVProgressHUD showErrorWithStatus:@"账号或密码不能为空!"];
            [SVProgressHUD dismissWithDelay:2.0f];
            return;
        }
    }
    else if(5 == btn.tag)
    {
        if ( isEmptyString(self.showReputationInfo.jdAccount)
            )
        {
            [SVProgressHUD showErrorWithStatus:@"账号或密码不能为空!"];
            [SVProgressHUD dismissWithDelay:2.0f];
            return;
        }
    }
    
    [SVProgressHUD showSuccessWithStatus:@"授权成功!"];
    [SVProgressHUD dismissWithDelay:2.0f];
}

- (void)submitALlAction:(UIButton *)btn
{
    if (isEmptyString(self.showReputationInfo.alipayAccount)
        || isEmptyString(self.showReputationInfo.alipayPwd)
        || isEmptyString(self.showReputationInfo.jdAccount)
        || isEmptyString(self.showReputationInfo.jdPwd))
    {
        [SVProgressHUD showErrorWithStatus:@"账号或密码不能为空!"];
        [SVProgressHUD dismissWithDelay:2.0f];
        return;
    }
    
   AuthenService *authenService = [[AuthenService alloc] init];
    [authenService saveReputationDetail:self.showReputationInfo
                           withCallBack:^(BOOL isSuccess) {
                               if (isSuccess)
                               {
                                   [[NSUserDefaults standardUserDefaults] setBool:YES 
                                                                           forKey:kXinYuAuthenRes];
                                   [SVProgressHUD showSuccessWithStatus:@"提交成功!"];
                                   [SVProgressHUD dismissWithDelay:2.0f];
                                   [self.navigationController popViewControllerAnimated:YES];
                               }
                               else
                               {
                                   [[NSUserDefaults standardUserDefaults] setBool:NO
                                                                           forKey:kXinYuAuthenRes];
                                   [SVProgressHUD showSuccessWithStatus:@"提交失败!"];
                                   [SVProgressHUD dismissWithDelay:2.0f];
                               }
                           }];
}



- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if ([[NSUserDefaults standardUserDefaults] boolForKey:kXinYuAuthenRes])
    {
        return NO;
    }
    return YES;
}


@end
